/****************************************************************************
 *
 *  LinuxChatty
 *  Copyright (C) 2010, David Hsu.
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 ***************************************************************************/
#ifndef _HTTPCLIENT_H_
#define _HTTPCLIENT_H_

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string>

/**
 * Class that will handle all web communications and sending/getting data
 */
class HTTPClient
{
public:
	/**
	 * Error enums for helping to debug
	 */
	enum Error
	{
		NoError,
		NoHost,
		ErrorCreatingSocket,
		UnableToConnectWithSocket,
		UnableToSendToSocket,
		ReceivedInvalidData,
		ConnectionTimedOut
	};
	
	HTTPClient();
	~HTTPClient();
	
	// Set the host that we're communicating with
	void SetHost(const std::string name);
	const std::string& GetHost();
	
	// Retrieve data received from the server
	const std::string& GetBuffer();
	
	// Get the server response
	const std::string GetResponse();
	
	// Get the server response body (without header info)
	const std::string GetBody();
	
	// Get last error to occur
	const Error GetRecentError();
	
	// Send/receive information to/from the host
	int CommDownload(const std::string path, const std::string& post_data,
	    const std::string auth = "");
	
private:
	// Helper function for clearing the latest error
	void ClearError();
	
	// Stores the host name and host information
	std::string host;
	struct hostent* hostent;
	
	// Holds the received information from the server
	std::string buffer;
	
	// Saves the last error encountered
	Error htmlError;
};

#endif
